package com.sakura.probe.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.probe.common.api.CommonResult;
import com.sakura.probe.pojo.PersonInfo;
import com.sakura.probe.service.PersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(tags = "AdminPersonController",description = "后台管理员的相关操作")
@Controller
@Slf4j
@RequestMapping("/backstage/person")
public class AdminController {
    @Autowired
    private PersonInfoService personService;

    @ApiOperation("根据条件显示管理员的信息")
    @GetMapping("/listAdminByCondition")
    @ResponseBody
    @RequiresRoles("1")
    public CommonResult getAdminList(Long pageIndex, Long pageSize, String name, Integer role){
        QueryWrapper<PersonInfo> wrapper = new QueryWrapper<>();
        if (null!=name){
            wrapper.like("username",name);
        }
        if (null!=role&&role!=0){
            wrapper.eq("role",role);
        }else {
            wrapper.notIn("role",0);
        }
        IPage<PersonInfo> page = personService.page(new Page<PersonInfo>()
                .setCurrent(pageIndex).setSize(pageSize), wrapper);
        return CommonResult.success(page);
    }

    @ApiOperation("修改管理员的权限")
    @PostMapping("/updateRole")
    @ResponseBody
    public CommonResult updateAdminRole(Long userId,Integer role){
        PersonInfo person = new PersonInfo();
        person.setUserId(userId).setRole(role);
        return personService.updateById(person)?CommonResult.success("修改成功"):CommonResult.failed("修改失败");
    }
}
